import{getToken} from "@/utils/auth.js"
//登录页面
const loginPage = "/pages/login"
//白名单
const whiteList = [
	"/pages/login",
]

function checkWhite(url){
	const path = url.split("?")[0]
	return whiteList.indexOf(path) !== -1
}

let list = ["navigateTo","redirectTo","reLaunch","switchTab"]

list.forEach(item => {
	uni.addInterceptor(item,{
		invoke(to) {
			if(getToken()){
				if(to.url===loginPage){
					uni.reLaunch({
						url:"/pages/home"
					})
					return true
				}
			} else{
				//白名单
				if(checkWhite(to.url)){
					return true
				}
				uni.reLaunch({url: loginPage})
				return false
			}
		},
		fail(err) {
			console.log(err);
		}
	})
})
